

estimation_data <-
  estimation_data %>%
  mutate(party_win_any = case_when(seats_won > 0 ~ "Party Won At Least One Seat",
                                   seats_won == 0 ~ "Party *Did Not* Win At Least One Seat",
                                   TRUE ~ NA_character_))



mod1 <- feols(avg_score_trust_elect ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party Won At Least One Seat", validated_election_outcome == "Not Elected"), fixed_effects = ~ID, cluster = "ID")
mod2 <- feols(avg_score_trust_elect ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party *Did Not* Win At Least One Seat", validated_election_outcome == "Not Elected"), fixed_effects = ~ID, cluster = "ID")
mod3 <- feols(avg_score_trust_elect ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party Won At Least One Seat", validated_election_outcome == "Elected"), fixed_effects = ~ID, cluster = "ID")

mod4 <- feols(avg_score_democ_support ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party Won At Least One Seat", validated_election_outcome == "Not Elected"), fixed_effects = ~ID, cluster = "ID")
mod5 <- feols(avg_score_democ_support ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party *Did Not* Win At Least One Seat", validated_election_outcome == "Not Elected"), fixed_effects = ~ID, cluster = "ID")
mod6 <- feols(avg_score_democ_support ~ pre_post, data = estimation_data %>% filter(party_win_any == "Party Won At Least One Seat", validated_election_outcome == "Elected"), fixed_effects = ~ID, cluster = "ID")




mods <- list(mod1, mod2, mod3, mod4, mod5, mod6)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)

fe_row <- tibble::tibble(
  term = "Individual FEs",
  !!!setNames(rep("\\checkmark", 6), names(table_df)[-1])
)

# Append the new row to the table
table_df <- bind_rows(table_df, fe_row)
# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:party_perf_const}Panel Analysis, Party Within-Constituency Performance",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)"), c("l", rep("c", 6))),
                    align = c("l", rep("c", 6)),
                    linesep = "") %>%
  kableExtra::row_spec(2, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      ">1 Seat" = 1,
      "<1 Seat" = 1,
      ">1 Seat" = 1,
      ">1 Seat" = 1,
      "<1 Seat" = 1,
      ">1 Seat" = 1
      
    ),
    align = c("l", rep("c", 6))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Not Elected" = 2,
      "Elected" = 1,
      "Not Elected" = 2,
      "Elected" = 1
    ),
    align = c("l", rep("c", 6))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Trust in Elec." = 3,
      "Supp. for Dem." = 3
    ),
    align = c("l", rep("c", 6))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a11.tex")
cat(latex_table, file = "./outputs/tables/table_a11.tex", append = TRUE)




